package com.taiyi.renaissancemall.ui.classification;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.FirstClassBean;
import com.taiyi.renaissancemall.bean.NextClassBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class ClassPresent extends BasePresent<ClassView> {

    public void getIndexClass() {
        HttpUtils.postRequest(BaseUrl.INDEX_FIRST_CLASS, this, "", new JsonCallback<BaseResultBean<FirstClassBean>>() {
            @Override
            public BaseResultBean<FirstClassBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FirstClassBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.indexClassSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    MyApp.getInstance().startActivity(new Intent(MyApp.getInstance(), LoginActivity.class));
                } else {
                    view.indexClassFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FirstClassBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getIndexNextClass(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("classificationid", id);
        HttpUtils.postRequest(BaseUrl.INDEX_NEXT_CLASS, this, map, new JsonCallback<BaseResultBean<NextClassBean>>() {
            @Override
            public BaseResultBean<NextClassBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<NextClassBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.indexNextClassSuccess(response.body().data);
                } else {
                    view.indexNextClassFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<NextClassBean>> response) {
                super.onError(response);
            }
        });
    }

}
